package cn.ddiancan.xddcloud.ddccore.response;

import java.util.List;

import cn.ddiancan.xddcloud.common.constants.CommonConstants;
import cn.ddiancan.xddcloud.common.entity.CommonResult;
import com.github.pagehelper.Page;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author ddc
 */
@Setter
@Getter
public class PagedResultListInfo<T> extends CommonResult<PagedResultListInfo.PagedEntity<T>> {

    @Getter(AccessLevel.NONE)
    @Setter(AccessLevel.NONE)
    private Page<T> page;

    private int pageNo = 1;

    private int pageSize = 15;

    private long total;

    private int pages;

    public PagedResultListInfo(int pageSize, int pageNo, List<T> records) {
        if (records instanceof Page<T>) {
            page = (Page<T>) records;
            this.pageNo = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.pages = page.getPages();
            this.total = page.getTotal();
            PagedEntity<T> pagedEntity = new PagedEntity();
            pagedEntity.setRecords(records);
            this.setData(pagedEntity);
            this.setCode(CommonConstants.SUCCESS);
            this.setMsg(CommonConstants.SUCCESS_MSG);
        }
    }

    @Setter
    @Getter
    public static class PagedEntity<T> {

        private List<T> records;
    }
}
